//
//  EmotionTool.m
//  Weibo
//
//  Created by Anasue on 9/30/15.
//  Copyright © 2015 vvusu. All rights reserved.
//
#define RecentEmotionFilePath [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]stringByAppendingPathComponent:@"recent_emotion.data"]

#import "EmotionTool.h"
#import "Emotion.h"
#import "MJExtension.h"

static NSArray *_defaultEmotions;
static NSArray *_emojiEmotions;
static NSArray *_lxhEmotions;
static NSMutableArray *_recentEmotions;

@implementation EmotionTool
#pragma mark - 懒加载
+ (NSArray *)defaultEmotions
{
    if (_defaultEmotions == nil) {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"EmotionIcons/default/info.plist" ofType:nil];
        _defaultEmotions = [Emotion objectArrayWithFile:path];
        [_defaultEmotions makeObjectsPerformSelector:@selector(setDirectory:) withObject:@"EmotionIcons/default"];
    }
    return _defaultEmotions;
}

+ (NSArray *)emojiEmotions
{
    if (_emojiEmotions == nil) {
        NSString *path = [[NSBundle mainBundle]pathForResource:@"EmotionIcons/emoji/info.plist" ofType:nil];
        _emojiEmotions = [Emotion objectArrayWithFile:path];
        [_emojiEmotions makeObjectsPerformSelector:@selector(setDirectory:) withObject:@"EmotionIcons/emoji"];
    }
    return _emojiEmotions;
}

+ (NSArray *)lxhEmotions
{
    if (_lxhEmotions == nil) {
        NSString *path = [[NSBundle mainBundle]pathForResource:@"EmotionIcons/lxh/info.plist" ofType:nil];
        _lxhEmotions = [Emotion objectArrayWithFile:path];
        [_lxhEmotions makeObjectsPerformSelector:@selector(setDirectory:) withObject:@"EmotionIcons/lxh"];
    }
    return _lxhEmotions;
}

+ (NSArray *)recentEmotions
{
    if (_recentEmotions == nil) {
        NSString *recentFilePath = RecentEmotionFilePath;
        _recentEmotions = [NSKeyedUnarchiver unarchiveObjectWithFile:recentFilePath];
        if (_recentEmotions == nil) {
            _recentEmotions = [NSMutableArray array];
        }
    }
    return _recentEmotions;
}

+ (void)addRecentEmotion:(Emotion *)emotion
{
    [self recentEmotions];
    [_recentEmotions removeObject:emotion];
    [_recentEmotions insertObject:emotion atIndex:0];
    [NSKeyedArchiver archiveRootObject:_recentEmotions toFile:RecentEmotionFilePath];
}

+ (Emotion *)emotionWithDesc:(NSString *)desc
{
    if (desc == nil) {
        return nil;
    }
    __block Emotion *foundEmotion = nil;
    [[self defaultEmotions] enumerateObjectsUsingBlock:^(Emotion *emotion, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([desc isEqualToString:emotion.chs] || [desc isEqualToString:emotion.cht]) {
            foundEmotion = emotion;
            *stop = YES;
        }
    }];
    if (foundEmotion) {
        return foundEmotion;
    }
    [[self lxhEmotions] enumerateObjectsUsingBlock:^(Emotion *emotion, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([desc isEqualToString:emotion.chs] || [desc isEqualToString:emotion.cht]) {
            foundEmotion = emotion;
            *stop = YES;
        }
    }];
    return foundEmotion;
}


@end
